
/**
 * @name MassWG (MASS Wind Gradient 1) Calculates surface moisture flux for
 * January 2-31, 1980 Part of Pacific Northwest Climate Data Analysis
 *
 * @version 0.1.504 Alpha, MASS Wind Gradient 1, 4th commit
 * @date 10/4/2013
 * @author Niko Simonson
 * @author Brett Yasutake
 */
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;

/**
 * @name StatusFrame
 */
public class StatusFrame extends javax.swing.JFrame {

    public static StatusModel sm;
    private Timer timer;
    private UIPrototype1 owner;
    String msg;
    boolean isRunning;
    Thread analysisThread;
    boolean initialized;
    


    /**
     * @name StatusFrame Creates new form StatusFrame
     */
    public StatusFrame(UIPrototype1 in) {
        initComponents();
        sm = new StatusModel();
        owner = in;
        timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timerActionPerformed(evt);
            }
        });
        isRunning = false;

        initialized = false;
    }

    /**
     * @name timerActionPerformed
     * @param evt
     */
    private void timerActionPerformed(java.awt.event.ActionEvent evt) {
        msg = sm.getCurrentMessage();

        if (statusMessageLbl.getText() != msg) {
            statusMessageLbl.setText(msg);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AlgorithmLbl = new javax.swing.JLabel();
        statusLbl = new javax.swing.JLabel();
        statusMessageLbl = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        runButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AlgorithmLbl.setText("Analysis: Moisture Flux");

        statusLbl.setText("Current Status:");

        statusMessageLbl.setText("Setting Up");

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        runButton.setText("Start Analysis");
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(runButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(closeButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(AlgorithmLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(statusLbl)
                                .addGap(18, 18, 18)
                                .addComponent(statusMessageLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 116, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AlgorithmLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusLbl)
                    .addComponent(statusMessageLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 301, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(runButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        // TODO add your handling code here:

        this.setVisible(false);
        owner.setVisible(true);

    }//GEN-LAST:event_closeButtonActionPerformed

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed
        // TODO add your handling code here:
        timer.start();
        if (analysisThread != null && !analysisThread.isAlive()) {
            isRunning = false;
        }

        if (!isRunning) {
            analysisThread = new Thread() {
                public void run() {
                    try {
                      
                        MassWG2.setFiles(owner.selectedFileNames);
                        MassWG2.initialize();

                        MassWG2.runAnalysis(sm, initialized);
                        initialized = true;

                    } catch (Exception ex) {

                        Logger.getLogger(StatusFrame.class.getName()).log(Level.SEVERE, null, ex);

                    }
                }
            };
            analysisThread.start();
            isRunning = true;
        }
    }//GEN-LAST:event_runButtonActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(StatusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(StatusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(StatusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(StatusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new StatusFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AlgorithmLbl;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton runButton;
    private javax.swing.JLabel statusLbl;
    private javax.swing.JLabel statusMessageLbl;
    // End of variables declaration//GEN-END:variables
}
